package www.wen.com.common.lib.base.activity

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import android.view.Window
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.navigation.fragment.findNavController
import com.dylanc.loadingstateview.LoadingStateView
import com.dylanc.loadingstateview.ViewType
import com.gyf.immersionbar.ImmersionBar
import www.wen.com.common.BR
import www.wen.com.common.R
import www.wen.com.common.lib.app.BaseApplication
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter
import www.wen.com.common.lib.util.ActivityManagerUtil
import www.wen.com.common.lib.util.KeyboardUtils
import www.wen.com.common.lib.view.OnMultipleClickListener


/**
 * @author WEN
 * @Description:
 * @date 2020/11/1 21:54
 */
abstract class BaseActivity<K : ViewDataBinding> : AppCompatActivity(), LoadingStateView.OnReloadListener {

    var mLoadingHelper: LoadingStateView? = null

    protected open lateinit var dataBinding: K

    /**
     * 设置 竖屏
     */
    protected open  var orientation: Int = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

    /**
     * 无标题栏
     */
    protected open  var featureId: Int = Window.FEATURE_NO_TITLE

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        super.setRequestedOrientation(orientation)
        dataBinding = DataBindingUtil.setContentView(this, getLayout())
        dataBinding.lifecycleOwner = this
        dataBinding.setVariable(BR.listener, mOnMultipleClickListener)

        //是否启用快捷标题栏
        isUseLoadingHelper()
        lazyInitView()
        initImmersionBar()
    }

    /**
     * 1秒内防重复点击
     */
    private val mOnMultipleClickListener = object : OnMultipleClickListener() {
        override fun onIClick(v: View) {
            this@BaseActivity.onClick(v)
        }
    }

    open fun onClick(v: View) {}

    abstract fun getLayout(): Int

    /**
     * 初始化沉浸式
     */
    open fun initImmersionBar() = ImmersionBar.with(this).init()

    abstract fun lazyInitView()

    override fun finish() {
        //防止activity关闭后键盘没有隐藏
        KeyboardUtils.hideSoftInput(this)
        super.finish()
    }

    private fun isUseLoadingHelper() {
        if (isEnableLoadingHelper()) {
            mLoadingHelper = getLoadingHelper().apply {
                configurationLoadingHelper(this)
                setOnReloadListener(this@BaseActivity)
            }
        }
    }

    open fun getLoadingHelper(): LoadingStateView {
        return LoadingStateView(dataBinding.root)
    }

    open fun configurationLoadingHelper(loadingHelper: LoadingStateView): LoadingStateView {
        loadingHelper.register(ViewType.TITLE, getTitleBarUniversalAdapter())
        loadingHelper.setDecorHeader(ViewType.TITLE)
        return loadingHelper
    }

    open fun getTitleBarUniversalAdapter(): LoadingStateView.ViewDelegate<*> =
        TitleBarUniversalAdapter(
            this,
            getString(R.string.logging_in),
            action = ::titleAction
        )

    open fun isEnableLoadingHelper() = false

    open fun titleAction(v: View) {
        when (v.id) {
            R.id.tvRight, R.id.ivRight -> {
                actionRight()
            }
            R.id.tvLeft, R.id.ivLeft -> {
                actionLeft()
            }
        }
    }

    open fun actionRight() {}

    open fun actionLeft() = finish()


}